package com.eastfair.imaster.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eastfair.boot.controller.SuperController;
import com.eastfair.boot.request.PageParams;
import com.eastfair.core.base.R;
import com.eastfair.echo.core.EchoService;
import com.eastfair.imaster.dto.VisitorUserDTO;
import com.eastfair.imaster.dto.VisitorUserPageQuery;
import com.eastfair.imaster.dto.VisitorUserSaveDTO;
import com.eastfair.imaster.dto.VisitorUserUpdateDTO;
import com.eastfair.imaster.entity.VisitorUser;
import com.eastfair.imaster.service.VisitorUserService;
import com.eastfair.imaster.vo.VisitorUserVO;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * <p>
 * 前端控制器
 * 观众用户信息表（根据显示需求灌入数据）
 * </p>
 *
 * @author zjn
 * @date 2021-08-25
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/visitorUser")
@Api(value = "VisitorUser", tags = "观众用户信息表（根据显示需求灌入数据）")
public class VisitorUserController extends SuperController<VisitorUserService, Long, VisitorUser, VisitorUserPageQuery, VisitorUserSaveDTO, VisitorUserUpdateDTO> {

    @Autowired
    private EchoService echoService;
    @Autowired
    private VisitorUserService visitorUserService;

    @Override
    public void handlerResult(IPage<VisitorUser> page) {
        // 想让返回值实现自动回显，请将此行代码打开
        // echoService.action(page);
    }

    /**
     * Excel导入后的操作
     *
     * @param list
     */
    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list) {
        List<VisitorUser> visitorUserList = list.stream().map((map) -> {
            VisitorUser visitorUser = VisitorUser.builder().build();
            //TODO 请在这里完成转换
            return visitorUser;
        }).collect(Collectors.toList());

        return R.success(baseService.saveBatch(visitorUserList));
    }

    @PostMapping("/getVisitorList")
    public R<Page<VisitorUserVO>> getVisitorList(@RequestBody PageParams<VisitorUserDTO> visitorUserDTOPageParams) {
        return R.success(visitorUserService.getVisitorList(visitorUserDTOPageParams));
    }

}